package com.example.hms.controller;

import com.example.hms.entity.User;
import com.example.hms.service.UserService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map, HttpServletRequest request) {
        String userId = map.get("userId");
        String password = map.get("userPassword");
        if(userId == null || password == null || userId.equals("") || password.isEmpty()) {
            return ResultSubmit.error("输入参数不全");
        }
        return userService.login(userId, password,request);
    }

    @RequestMapping(value = "/login2",method = RequestMethod.GET)
    public Result login2(String userId,String password, HttpServletRequest request) {
        return userService.login(userId, password,request);
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result info(HttpServletRequest request) {
        return userService.info(request);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("token",null);
        session.setAttribute("userId",null);
        return ResultSubmit.success("退出成功");
    }

    @RequestMapping(value = "/selectAllUser",method = RequestMethod.GET)
    public Result selectAllUser(String userId, String userName, String roleName) {
        return userService.selectAllUser(userId, userName, roleName);
    }

    @RequestMapping(value = "/查询所有用户信息",method = RequestMethod.GET)
    public Result 查询所有用户信息(String 用户ID, String 用户名, String 角色名) {
        return userService.查询所有用户信息(用户ID, 用户名, 角色名);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    public Result insert(User user) {
        return userService.insert(user);
    }

    @RequestMapping(value = "/插入",method = RequestMethod.GET)
    public Result 插入(User 用户) {
        return userService.插入(用户);
    }

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public Result update(User user) {
        return userService.update(user);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(String userId) {
        return userService.delete(userId);
    }
}
